﻿using System;
using System.Linq.Expressions;
using System.Reflection;

namespace CodeStash.Utils
{
    public static class ExpressionUtils
    {

        public static string GetPropertyName<TModel, TItem>(
            Expression<Func<TModel, TItem>> propertyExpression)
        {
            var lambda = propertyExpression as LambdaExpression;
            MemberExpression memberExpression;
            if (lambda.Body is UnaryExpression)
            {
                var unaryExpression = lambda.Body as UnaryExpression;
                memberExpression = unaryExpression.Operand as MemberExpression;
            }
            else
            {
                memberExpression = lambda.Body as MemberExpression;
            }

            var propertyInfo = memberExpression.Member as PropertyInfo;

            return propertyInfo.Name;
        }

    }
}